package com.takeshequ.chat;

import net.tsz.afinal.FinalDb;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.sax.StartElementListener;
import cn.jpush.android.api.JPushInterface;

import com.google.gson.Gson;
import com.takeshequ.Constants;
import com.takeshequ.friends.BaseData;
import com.utils.AndroidUtil;
import com.utils.Logger;

public class TalkReceiver extends BroadcastReceiver {
	private static final String TAG = "TalkReceiver";

	private NotificationManager nm;

	@Override
	public void onReceive(Context context, Intent intent) {
		System.out.println("获得了receive");
		if (null == nm) {
			nm = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);
		}

		Bundle bundle = intent.getExtras();
		Logger.d(TAG, "onReceive - " + intent.getAction() + ", extras: "
				+ AndroidUtil.printBundle(bundle));

		if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
			Logger.d(TAG, "JPush用户注册成功");

		} else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent
				.getAction())) {
			Logger.d(TAG, "接受到推送下来的自定义消息");

			// Push Talk messages are push down by custom message format
			processCustomMessage(context, bundle);

		} else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent
				.getAction())) {
			Logger.d(TAG, "接受到推送下来的通知");

		} else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent
				.getAction())) {
			Logger.d(TAG, "用户点击打开了通知");

		} else {
			Logger.d(TAG, "Unhandled intent - " + intent.getAction());
		}

	}

	private void processCustomMessage(Context context, Bundle bundle) {
		String title = bundle.getString(JPushInterface.EXTRA_TITLE);
		String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
		System.out.println(message);
		Gson gson = new Gson();
		TalkHistory talkHistory = gson.fromJson(message, TalkHistory.class);
		talkHistory.setTalkGroupId(talkHistory.getTargetId() + ","
				+ talkHistory.getFromId());
		if (talkHistory.getTargetType().equals("0")) {
			FinalDb db = FinalDb.create(context);
			db.save(talkHistory);
			ActivityManager am = (ActivityManager) context
					.getSystemService(Context.ACTIVITY_SERVICE);
			ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
			if ("com.takeshequ.chat.TalkActivity".equals(cn.getClassName())) {
				System.out.println("正在聊天");
				// 正在聊天
				Intent msgIntent = new Intent(
						TalkActivity.MESSAGE_RECEIVED_ACTION);
				context.sendBroadcast(msgIntent);
			} else {
				// 提醒
				String nickName = "陌生人";
				Constants.firend = BaseData.getFriend(talkHistory.getFromId());
				nickName = Constants.firend.getNickName();
				NotificationHelper.showMessageNotification(context, nm, "踏客社区",
						talkHistory.getContent(), nickName);

			}
		}else if(talkHistory.getTargetType().equals("1")){
			NotificationHelper.showMessageNotification(context, nm, "踏客社区",
					talkHistory.getContent(), "");
		}
	}

}
